const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      qiniu: constant.resource.qiniu,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/achievement?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            achievement: res.data.data.achievement,
            studentName: res.data.data.studentName,
            startPhoto: res.data.data.achievement.start_photo ? res.data.data.achievement.start_photo : null,
            startUploadTime: res.data.data.achievement.start_upload_time ? res.data.data.achievement.start_upload_time : null,
            endPhoto: res.data.data.achievement.end_photo ? res.data.data.achievement.end_photo : null,
            endUploadTime: res.data.data.achievement.end_upload_time ? res.data.data.achievement.end_upload_time : null
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  uploadPhoto: function (e) {
    var suffix = e.currentTarget.dataset.suffix;
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#000000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album', suffix)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera', suffix)
          }
        }
      }
    })
  },

  chooseWxImage: function (type, suffix) {
    var that = this;
    //var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        wx.uploadFile({
          url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/upload-contrast-photo?mobile=' + that.data.mobile + '&suffix=' + suffix,
          filePath: res.tempFilePaths[0],
          name: 'photo',
          success(res) {
            var data = JSON.parse(res.data);
            if (data.code == "0") {
              wx.showToast({
                title: '图片上传成功',
                icon: 'success',
                duration: 1000
              })

              if(suffix == 'before') {
                that.setData({
                  startPhoto: data.data.src,
                  startUploadTime: data.data.uploadTime
                })
              } else if(suffix == 'after') {
                that.setData({
                  endPhoto: data.data.src,
                  endUploadTime: data.data.uploadTime
                })
              }
            } else {
              wx.showToast({
                title: data.message,
                icon: 'none',
                duration: 1000
              })
            }    
          },
          fail(res) {
            wx.showToast({
              title: '图片上传失败',
              icon: 'none',
              duration: 1000
            })
          }
        })
      }
    })
  }
})